import { _decorator,Node, instantiate, Prefab } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { AbNames, EventMsgKes } from '../../Config/ResConfig';
import { ResManager } from '../../../../Framework/Scripts/Managers/ResManager';
import { GamePlayMap } from '../GamePlay/GamePlayMap';
import { IGetRecoreRes, IRecordZt } from '../proto/gameZt';
import { Answer } from '../../Config/GameCfg';
const { ccclass, property } = _decorator;

@ccclass('GamePlayRecord_Ctr')
export class GamePlayRecord_Ctr extends UIComponent {
    records:IRecordZt[];
    idx: number;
    nodeL: Node;
    nodeR: Node;
    ts_map: GamePlayMap;

    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();
        this.setBgAni(0);

        this.nodeL = this.ViewNode('btnL');
        this.nodeR = this.ViewNode('btnR');

        let map = this.ViewNode('map');
        let pre = ResManager.inst.getAsset(AbNames.GamePlay,'Prefabs/GamePlay/GameMap',Prefab);
        let item: any = instantiate(pre)
        item.parent = map;
        this.ts_map = item.addComponent(GamePlayMap) as GamePlayMap;

        this.AddButtonListener('btnBack',this,this.OnBtnCloseClick);
        this.AddButtonListener('btnL',this,this.onBtnLeftClick);
        this.AddButtonListener('btnR',this,this.onBtnRightClick);
    }

    private InitData(){
        this.idx = 0;
        this.records = [];
   
        //请求答题记录
        globalThis.gameMgr._GameZtMsg.sendGetRecord();
    }

    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgThemeMatchGameRecordRes,this.onThemeMatchGameRecordRes,this);
    }

    private unRegisterEvent(){
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgThemeMatchGameRecordRes,this.onThemeMatchGameRecordRes,this);
    }

    private onBtnLeftClick(){
        this.idx--;
        this.upDataMap();
    }

    private onBtnRightClick(){
        this.idx++;
        this.upDataMap();
    }

    private updatBtnStatus(){
        if(this.idx > this.records.length -1){
            this.nodeR.active = false;
            this.idx--;
        }else if(this.idx === this.records.length-1){
            this.nodeL.active = true;
            this.nodeR.active = false;
        }else if(this.idx < 0){
            this.nodeL.active = false;
            this.idx++;
        }else if(this.idx === 0){
            this.nodeL.active = false;
            this.nodeR.active = true;
        }else if(this.idx > 0 && this.idx < this.records.length){
            this.nodeR.active = true;
            this.nodeL.active = true;
        }
    }

    private upDataMap(){
        this.updatBtnStatus();
        let d:IRecordZt = this.records[this.idx];
        this.ts_map.showTopicRecord(d.tid,this.idx);
        let isTrue = d.status === Answer.Cur ? true : false;
        this.ts_map.setChoicesData(d.cid,isTrue,true);
    }

    private onThemeMatchGameRecordRes(data:IGetRecoreRes){
        console.log('========onThemeMatchGameRecordRes========',data);
        let records = data.list;
        if(records){
            this.records = Array.from(records);
            this.upDataMap()
        }
    }
}


